home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / bm_rm.zip / PUTS.ASM < prev    next >
Assembly Source File  |  1990-05-23  |  4KB  |  129 lines

  1. ;Write a character string to standard output.
  2. ;
  3. ;Syntax: int puts  (char *string); -- Write an ASCIIZ string and append newline
  4. ;        int putsc (char *string); -- Just write an ASCIIZ string as is
  5. ;        void putchar (char c);    -- Write a character
  6. ;        void newline (void);      -- Print carriage return and linefeed
  7. ;
  8. ;Note: putchar() does not expand '\n' to "\r\n" whereas puts() and putsc() do.
  9. ;
  10. ;Return value:  0 = no errors detected
  11. ;
  12. ;Copyright (C) 1989, 1990 Brian B. McGuinness
  13. ;                         15 Kevin Road
  14. ;                         Scotch Plains, NJ 07076
  15. ;
  16. ;These functions are free software; you can redistribute them and/or modify them
  17. ;under the terms of the GNU General Public License as published by the Free 
  18. ;Software Foundation; either version 1, or (at your option) any later version.
  19. ;
  20. ;These functions are distributed in the hope that they will be useful, but 
  21. ;WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
  22. ;FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  23. ;details.
  24. ;
  25. ;You should have received a copy of the GNU General Public License along with 
  26. ;these functions; if not, write to the Free Software Foundation, Inc., 675 Mass 
  27. ;Ave, Cambridge, MA 02139, USA.
  28. ;
  29. ;Assemble this code with Microsoft Macro Assembler version 5.1 or higher:
  30. ;MASM /Mx puts;
  31. ;
  32. ;Version 1.0, August 1989 - Original version
  33. ;Version 1.1, May 1990    - Add putsc() and putchar() functions
  34.  
  35.         DOSSEG
  36.         .MODEL small,C
  37.         .DATA
  38. crlf    db 13, 10, '$'  ;Carriage return and linefeed.
  39.         .CODE
  40.  
  41.  
  42. ;-------------------------------------------------------------------------------
  43. ;puts() - Print an ASCIIZ string, then append a newline.
  44.  
  45. puts    proc proc uses si, string:ptr
  46. if @DataSize
  47.         lds si,string           ;Far pointer
  48. else
  49.         mov si,string           ;Near pointer
  50. endif
  51.         call doputs
  52.         call newline
  53.         ret
  54. puts    endp
  55.  
  56. ;-------------------------------------------------------------------------------
  57. ;putsc() - Print an ASCIIZ string, converting newline chars to CR, LF pairs.
  58.  
  59. putsc   proc uses si, string:ptr
  60. if @DataSize
  61.         lds si,string           ;Far pointer
  62. else
  63.         mov si,string           ;Near pointer
  64. endif
  65.         call doputs
  66.         ret
  67. putsc   endp
  68.  
  69. ;-------------------------------------------------------------------------------
  70. ;doputs() - Print the ASCIIZ string pointed to by SI.
  71.  
  72. doputs  proc uses bx dx
  73.         mov ax,4400H    ;Get device information for stdout.
  74.         mov bx,1
  75.         int 21H
  76.         xor dh,dh       ;Save this information so we can restore it later.
  77.         push dx
  78.  
  79.         test dl,80H     ;Check if it is a character device rather than a file.
  80.         jz @F
  81.         mov ax,4401H    ;It is (stdout wasn't redirected), so set raw mode on.
  82.         or dl,00100000B
  83.         int 21H
  84.  
  85. @@:     cld
  86. prtloop:lodsb
  87.         or al,al        ;Zero byte marks end of string.
  88.         jz exit
  89.  
  90.         cmp al,10       ;Expand '\n' to "\r\n".
  91.         jne normal
  92.         call newline
  93.         jmp short prtloop
  94.  
  95. normal: mov ah,2        ;Normal character: write it to stdout.
  96.         mov dl,al
  97.         int 21H
  98.         jmp short prtloop
  99.  
  100. exit:   mov ax,4401H    ;Restore original mode for stdout.
  101.         mov bx,1
  102.         pop dx
  103.         int 21H
  104.  
  105.         xor ax,ax       ;Exit with error code zero.
  106.         ret
  107. doputs  endp
  108.  
  109. ;-------------------------------------------------------------------------------
  110. ;Print a carriage return and a linefeed.
  111.  
  112. newline proc uses dx
  113.         mov dx,offset crlf
  114.         mov ah,9
  115.         int 21H
  116.         ret
  117. newline endp
  118.  
  119. ;-------------------------------------------------------------------------------
  120. ;Print a single character.
  121.  
  122. putchar proc uses dx, c:word
  123.         mov ah,2
  124.         mov dx,c
  125.         int 21H
  126.         ret
  127. putchar endp
  128.         end
  129.